﻿using LD.Common;
using LD.Host;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YTE.Control;

namespace LD.BC
{
    public partial class FrmIoManualEx : FrmBase
    {
        private string id = Guid.NewGuid().ToString();
        public FrmIoManualEx()
        {
            InitializeComponent();
            GetLang(this.Controls);
            this.FormClosing += FrmIoManual_FormClosing;
            this.Shown += FrmIoManual_Shown;
        }
        private void GetLang(Control.ControlCollection _controls)
        {
            foreach (Control control in _controls)
            {
                GetLang(control.Controls);
                if (control is BigLabel)
                {
                    control.Text = Language.GetString("FrmIoManual." + control.Name);
                }
            }
        }
        private void FrmIoManual_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();

            PubSub.Hub.Default.Unsubscribe(id);
        }
        private void FrmIoManual_Shown(object sender, EventArgs e)
        {
            Init();
            timer1.Start();
            //Subscribe();
        }
        private void Monitor()
        {
            #region IO
            int index = 1;
            var _in = ETHModbus.ReadIn();
            if (_in == null)
            {
                timer1.Stop();
                return;
            }
            //in
            for (int i = 0; i < _in.Count; i++)
            {
                var control = this.panel_di.Controls.Find("di" + index, true);
                if (control.Count() > 0)
                {
                    if (control[0] != null && control[0].GetType() == typeof(IoControlEx))
                    {
                        var ioControl = (control[0] as IoControlEx);
                        if (!this.IsDisposed)
                        {
                            ioControl.Signal = _in[i] ? 0 : 1;
                        }
                    }
                    else
                        Console.WriteLine("IO控件未找到");
                }
                index++;
            }

            //out
            index = 0;
            var _out = ETHModbus.ReadOut();
            for (int i = 0; i < _out.Count; i++)
            {
                var control = this.panel_di.Controls.Find("do" + index, true);
                if (control.Count() > 0)
                {
                    if (control[0] != null && control[0].GetType() == typeof(IoControlEx))
                    {
                        var ioControl = (control[0] as IoControlEx);
                        if (!this.IsDisposed)
                        {
                            ioControl.Signal = _out[i] ? 0 : 1;
                        }
                    }
                    else
                        Console.WriteLine("IO控件未找到");
                }
                index++;
            }
            #endregion
        }
        private void Init()
        {
            List<TB_IO> io_in = new List<TB_IO> {
                new TB_IO() { IoType=(int)IoType.输入,No=1,Name="检测OK"},
                new TB_IO() { IoType=(int)IoType.输入,No=2,Name="检测NG"},
                new TB_IO() { IoType=(int)IoType.输入,No=3,Name="准备"},
                new TB_IO() { IoType=(int)IoType.输入,No=4,Name="备用"},
            };

            int x = 5; int y = 40;

            foreach (var item in ETHModbus.GetIn)
            {
                var control = GetIoControl("di" + item.No, item.No, item.Name);
                control.IoType = IoType.输入;
                control.Size = new Size(240, 40);
                control.BorderStyle = BorderStyle.FixedSingle;
                control.Location = new Point(x, y);
                this.panel_di.Controls.Add(control);
                y += 45;
            }
            List<TB_IO> io_out = new List<TB_IO> {
                new TB_IO() { IoType=(int)IoType.输出,No=1,Name="激光检测"},
                new TB_IO() { IoType=(int)IoType.输出,No=2,Name="程序0"},
                new TB_IO() { IoType=(int)IoType.输出,No=3,Name="程序1"},
                new TB_IO() { IoType=(int)IoType.输出,No=4,Name="程序2"},
                new TB_IO() { IoType=(int)IoType.输出,No=5,Name="程序3"},
                new TB_IO() { IoType=(int)IoType.输出,No=6,Name="程序4"},
                new TB_IO() { IoType=(int)IoType.输出,No=7,Name="程序5"},
            };
            x = 250; y = 40;
            foreach (var item in ETHModbus.GetOut)
            {
                var control = GetIoControl("do" + item.No, item.No, item.Name);
                control.BorderStyle = BorderStyle.FixedSingle;
                control.Size = new Size(240, 40);
                control.IoType = IoType.输出;
                control.Location = new Point(x, y);
                this.panel_di.Controls.Add(control);
                y += 45;
            }
        }
        private IoControlEx GetIoControl(string name, int no, string ioName)
        {
            IoControlEx control = new IoControlEx();
            control.Name = name;
            control.No = no;
            control.IoName = ioName;
            control.Size = new Size(290, 40);
            return control;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            Monitor();
            timer1.Start();
        }
    }
}
